﻿using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DevSamPle.FormPlugin
{
    [Description("【表单插件】打印表单插件"), HotUpdate]
    public class ShowFormPlugin : AbstractDynamicFormPlugIn
    {
        public override void AfterBindData(EventArgs e)
        {
            base.AfterBindData(e);
            var eventsProxyProperty = this.View.GetType().GetProperties(BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(o => o.Name == "EventsProxy");

            if (eventsProxyProperty != null)

            {

                var eventsProxy = eventsProxyProperty.GetValue(this.View, null) as DynamicFormViewPlugInProxy;

                if (eventsProxy != null)

                {

                    var msg = string.Join("\r\n", eventsProxy.PlugIns.Select(o =>

                    {

                        var type = o.GetType();

                        if (type == typeof(PythonPlugIn))

                        {

                            var scriptStringField = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(p => p.Name == "scriptString");

                            if (scriptStringField != null)

                            {

                                return string.Format("【{0}】\r\n【Begin】\r\n{1}\r\n【End】", type.Name, scriptStringField.GetValue(o));

                            }

                        }

                        return type.FullName;

                    }));

                    this.View.ShowMessage(msg);

                }

            }
        }
    }
}
